package com.bckj.fastboot.flow.configuration;

import com.bckj.fastboot.core.extra.extpt.ExtPtProcHandler;
import com.bckj.fastboot.core.extra.stage.StageRepository;
import com.bckj.fastboot.flow.process.BpmnProcessExecutor;
import com.bckj.fastboot.flow.process.BpmnProcessProvider;
import com.bckj.fastboot.flow.process.ExtPtProcHandlerImpl;
import com.bckj.fastboot.flow.process.consumer.StageBpmnProcessConsumer;
import com.bckj.fastboot.flow.process.expression.CustomExpressionProcessor;
import com.bckj.fastboot.flow.repository.FastProcessRepository;
import com.bckj.fastboot.flow.repository.impl.DefaultFastProcessRepository;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
@ConditionalOnProperty(prefix = "flow.bpmn", name = "enabled", havingValue = "true")
public class FlowBpmnProcessConfiguration {

    @Bean
    @ConditionalOnMissingBean(FastProcessRepository.class)
    FastProcessRepository fastProcessRepository(JdbcTemplate flowJdbcTemplate) {
        return new DefaultFastProcessRepository(flowJdbcTemplate);
    }

    @Bean
    public BpmnProcessProvider bpmnProcessProvider(FastProcessRepository fastProcessRepository) {
        return new BpmnProcessProvider(fastProcessRepository);
    }

    @Bean
    public CustomExpressionProcessor customExpressionProcessor() {
        return new CustomExpressionProcessor();
    }

    @Bean
    public BpmnProcessExecutor bpmnProcessExecutor(BpmnProcessProvider bpmnProcessProvider) {
        return new BpmnProcessExecutor(bpmnProcessProvider);
    }

    @Bean
    @ConditionalOnBean(StageRepository.class)
    public StageBpmnProcessConsumer stageBpmnProcessConsumer(StageRepository stageRepository) {
        return new StageBpmnProcessConsumer(stageRepository);
    }

    @Bean
    @ConditionalOnBean(StageBpmnProcessConsumer.class)
    public ExtPtProcHandler extPtProcHandler(BpmnProcessExecutor bpmnProcessExecutor, StageBpmnProcessConsumer stageBpmnProcessConsumer) {
        return new ExtPtProcHandlerImpl(bpmnProcessExecutor, stageBpmnProcessConsumer);
    }
}
